﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRO_DOCS.DocumentManagementSystem.ADMIN.WIN.Configurations.EmailsAndWorkflows
{
    public partial class WorkFlowSystemSettings : BLL.WIN.Templates.DevxConfigurationUserControlTemplate
    {
        bool WorkflowSettingsAdded
        { get; set; }

        Dictionary<int, int> ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHIES
        { get; set; }

        public WorkFlowSystemSettings()
        {
            InitializeComponent();

            btnAddNew.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            btnDeleteItem.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            btnSaveUpdate.ItemClick += btnSaveUpdate_ItemClick;
            btnRefresh.ItemClick += btnRefresh_ItemClick;

            //Load storage medias
            ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHIES = new Dictionary<int, int>();
            comboDepartmentStorageMedia.Properties.Items.BeginUpdate();
            comboDepartmentStorageMedia.Properties.Items.Clear();
            int i = 0;
            PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHIESLoadAll().ToList().ForEach(new Action<PRO_DOCS.DAL.Database.proc_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHIESLoadAllResult>(delegate(PRO_DOCS.DAL.Database.proc_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHIESLoadAllResult _sTORAGE_HEIRARCHY)
            {
                ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHIES.Add(i, _sTORAGE_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID);
                comboDepartmentStorageMedia.Properties.Items.Add(_sTORAGE_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_NAME);
                i++;
            }));

            comboDepartmentStorageMedia.Properties.Items.EndUpdate();
            comboDepartmentStorageMedia.SelectedIndex = -1;

            LOAD_APPLICATION_WORKFLOW_SETTINGS();

        }

        private void LOAD_APPLICATION_WORKFLOW_SETTINGS()
        {
            PRO_DOCS.DAL.Database.APPLICATION_WORKFLOW_SETTING APPLICATION_WORKFLOW_SETTING = PRO_DOCSDatabaseDataContext.APPLICATION_WORKFLOW_SETTINGs.FirstOrDefault();

            if (APPLICATION_WORKFLOW_SETTING != null)
            {
                comboDepartmentStorageMedia.Text = APPLICATION_WORKFLOW_SETTING.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHy.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_NAME;
                chkSendEmailConfirmation.Checked = APPLICATION_WORKFLOW_SETTING.APPLICATION_WORKFLOW_SETTING_SEND_NOTIFICATION_BY_MAIL.Value;
                WorkflowSettingsAdded = true;
            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LOAD_APPLICATION_WORKFLOW_SETTINGS();
        }

        private void btnSaveUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            dxErrorProvider1.ClearErrors();

            bool data_accepted = comboDepartmentStorageMedia.SelectedIndex > -1;

            if (data_accepted)
            {
                if (WorkflowSettingsAdded)
                {
                    PRO_DOCSDatabaseDataContext.proc_APPLICATION_WORKFLOW_SETTINGSUpdate
                        (
                        ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHIES[comboDepartmentStorageMedia.SelectedIndex],
                        chkSendEmailConfirmation.Checked
                        );
                }
                else
                {
                    PRO_DOCSDatabaseDataContext.proc_APPLICATION_WORKFLOW_SETTINGSInsert
                                            (
                                            ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHIES[comboDepartmentStorageMedia.SelectedIndex],
                                            chkSendEmailConfirmation.Checked
                                            );
                    WorkflowSettingsAdded = true;
                }
            }
            else
            {
                dxErrorProvider1.SetError(comboDepartmentStorageMedia, BLL.WIN.Properties.ErrorProviderMessages.Default.InputLeftBlank.Replace("{name}", "Storage media"));
            }
        }
    }
}
